/**   
* @Title: LoadCityCode.java 
* @author zengshuai shi   
* @date 2015年8月4日 上午10:36:47 
*/
package com.shanghaionstar.utils;

import android.content.Context;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/** 
* @ClassName: LoadCityCode 
* @Description: TODO
* @author zengshuai shi
* @date 2015年8月4日 上午10:36:47 
*/
public class LoadCityCode {
	private InputStream inputStream;
	List<String[]> listcity;

	public LoadCityCode(Context pcontext) {
		try {
			inputStream = pcontext.getAssets().open("citycode.txt");
			listcity = getListArrTxt(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				inputStream = null;
			}
		}
	}

	/**
	 * 
	* @Title: findCityCodeByZhCity 
	* @Description: 通过中文城市查找城市Code
	* @param @param city
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	public String findCityCodeByZhCity(String city) {
		if (city == null || "".equalsIgnoreCase(city)) {
			return "";
		}
		for (String[] s : listcity) {
			if ((city.trim()).equalsIgnoreCase(s[1].trim())) {
				return s[0];
			}
		}
		return city;
	}

	/**
	 * 
	* @Title: findZhCityByCityCode 
	* @Description: 通过城市代码查找中文城市
	* @param @param city
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	public String findZhCityByCityCode(String city) {
		if (city == null || "".equalsIgnoreCase(city)) {
			return "";
		}
		for (String[] s : listcity) {
			if ((city.trim()).equalsIgnoreCase(s[0].trim())) {
				return s[1];
			}
		}
		return city;
	}

	/**
	 * 
	* @Title: getListArrTxt 
	* @Description: list 数组
	* @param @param pInputStream
	* @param @return    设定文件 
	* @return List<String[]>    返回类型 
	* @throws
	 */
	public List<String[]> getListArrTxt(InputStream pInputStream) {
		List<String[]> list = new ArrayList<String[]>();
		String str = null;
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(pInputStream, "UTF-8"));
			while ((str = br.readLine()) != null) {
				String[] s1 = str.split(",");
				list.add(s1);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return list;
	}
}
